package com.wen.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wen.common.Result;
import com.wen.entity.ConsumRecord;
import com.wen.entity.Member;
import com.wen.entity.Course;
import com.wen.entity.MemberOrder;
import com.wen.service.ConsumRecordService;
import com.wen.service.CourseService;
import com.wen.service.MemberService;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @author dhh
 * @date 2021/11/18 11:17
 * 消费记录
 */
@RestController
@AllArgsConstructor
@RequestMapping("/consumRecord")
public class ConsumRecordController {

    private final ConsumRecordService consumRecordService;

    private final MemberService memberService;

    private final CourseService courseService;

    //    添加信息
    @PostMapping
    public Result<?> save(@RequestBody ConsumRecord consumRecord) {

        LambdaQueryWrapper<Member> wrapper = Wrappers.<Member>lambdaQuery();
        wrapper.eq(Member::getId, consumRecord.getMemberId());
        Member member=memberService.getOne(wrapper);

        LambdaQueryWrapper<Course> wrapper1 = Wrappers.<Course>lambdaQuery();
        wrapper1.eq(Course::getId, consumRecord.getCourseId());
        Course course=courseService.getOne(wrapper1);

        member.setBalance(member.getBalance()-course.getCoursePrice());
        memberService.updateById(member);

        consumRecordService.save(consumRecord);
        return Result.success();
    }

    //    更新
    @PutMapping
    public Result<?> update(@RequestBody ConsumRecord consumRecord) {
        consumRecordService.updateById(consumRecord);
        return Result.success();
    }

    //    s删除
    @DeleteMapping("/{id}")
    public Result<?> delete(@PathVariable Long id) {
        consumRecordService.removeById(id);
        return Result.success();
    }

    //    查询
    @GetMapping
    public Result<?> findPage(@RequestParam(defaultValue = "1") Integer pageNum,
                              @RequestParam(defaultValue = "10") Integer pageSize,
                              @RequestParam(defaultValue = "") String search) {
        LambdaQueryWrapper<ConsumRecord> wrapper = Wrappers.<ConsumRecord>lambdaQuery();
        Page<ConsumRecord> NewsPage = consumRecordService.page(new Page<>(pageNum, pageSize), wrapper);
        return Result.success(NewsPage);
    }

    @GetMapping("/getCourseCopy")
    public Result<?> getCourseCopy(@RequestParam Integer courseId,
                                   @RequestParam Integer memberId) {
        LambdaQueryWrapper<ConsumRecord> wrapper = Wrappers.<ConsumRecord>lambdaQuery();
        if (courseId != null) {
            wrapper.like(ConsumRecord::getCourseId, courseId);
        }
        if (memberId != null) {
            wrapper.like(ConsumRecord::getMemberId, memberId);
        }
        List<Map<String, Object>> mapList = consumRecordService.listMaps(wrapper);
        return Result.success(mapList);
    }

    @GetMapping("/getConsumRecord")
    public Result<?> getConsumRecord(@RequestParam Integer memberId, @RequestParam String search) {
        List<Map<String, Object>> mapList = consumRecordService.getConsumRecord(memberId, search);
        return Result.success(mapList);
    }
}
